package com.zhuss.food.processor;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.product.CreateProductBo;
import com.zhuss.common.AbstractBo;
import com.zhuss.common.ResponseSucc;
import com.zhuss.core.db.dao.ext.TempDao;
import com.zhuss.core.db.entity.Temp;
import com.zhuss.enums.ImportType;
import com.zhuss.enums.ProductType;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.entity.Food;
import com.zhuss.util.DateUtil;
import com.zhuss.util.ExcelUtil;
import com.zhuss.util.FoodUtil;
import com.zhuss.util.NoUtil;


@Component("weekMenuImport")
public class WeekMenuImport {
	
	@Autowired
	private TempDao tempDao;
	
	@Autowired
	private FoodRead foodRead;
	
	@Autowired
    private ProductCreate productCreate;
	
	public Object process(List<String[]> resultList, HttpServletResponse response, Integer type, Integer week) throws Exception {
		if (ImportType.导入.sameKey(type)) {
			AbstractBo abstractBo = new AbstractBo();
			Integer area = abstractBo.getConcurrentArea();
			
			List<Temp> tempList = new ArrayList<Temp>();
			
			String batchNubmer = NoUtil.gen();
			Date now = new Date();
			
			Map<String, Food> foodMap = foodRead.listMap(area);
			Map<String, Date> weekDayMap = DateUtil.toWeekMap(area, week);
			Map<String, Integer> floorMap = FoodUtil.getFloorMap(area);
			Map<String, Integer> foodTimeMap = FoodUtil.getFoodTimeMap();
			
			for (int i = 0; i < resultList.size(); i ++) {
				if (i == 0) {
					continue;
				}
				String[] item = resultList.get(i);
				
				String item0 = item[0];
				String item1 = item[1];
				String item2 = item[2];
				String item3 = item[3];
				
				
				Temp temp = new Temp();
				temp.setItem0(item0);
				temp.setItem1(item1);
				temp.setItem2(item2);
				temp.setItem3(item3);
				temp.setAccount(abstractBo.getConcurrentUserName());
				temp.setBatchNumber(batchNubmer);
				temp.setCreateTime(now);
				
				String foodName = item0;
				String weekDayName = item1;
				String floorName = item2;
				String productTimeName = item3;
				
				if (foodName == null || "".equals(foodName.trim())) {
					temp.setReason(ExceptionCode.菜品名字为空.getMsg() + ": " + foodName);
					tempDao.insert(temp);
					tempList.add(temp);
					continue;
				}
				if (weekDayName == null || "".equals(weekDayName.trim())) {
					temp.setReason(ExceptionCode.周几为空.getMsg() + ": " + weekDayName);
					tempDao.insert(temp);
					tempList.add(temp);
					continue;
				}
				if (floorName == null || "".equals(floorName.trim())) {
					temp.setReason(ExceptionCode.楼层为空.getMsg() + ": " + floorName);
					tempDao.insert(temp);
					tempList.add(temp);
					continue;
				}
				if (floorName == null || "".equals(floorName.trim())) {
					temp.setReason(ExceptionCode.餐类型为空.getMsg() + ": " + floorName);
					tempDao.insert(temp);
					tempList.add(temp);
					continue;
				}
				
				Food food = foodMap.get(foodName);
				
				if (food == null) {
					temp.setReason(ExceptionCode.菜品不存在.getMsg() + ": " + foodName);
					tempDao.insert(temp);
					tempList.add(temp);
					continue;
				}
				
				Date date = weekDayMap.get(weekDayName);
				if (date == null) {
					temp.setReason(ExceptionCode.周几不存在.getMsg() + ": " + weekDayName);
					tempDao.insert(temp);
					tempList.add(temp);
					continue;
				}
				
				Integer floor = floorMap.get(floorName);
				if (floor == null) {
					temp.setReason(ExceptionCode.楼层不存在.getMsg() + ": " + floorName);
					tempDao.insert(temp);
					tempList.add(temp);
					continue;
				}
				
				Integer foodTime = foodTimeMap.get(productTimeName);
				if (foodTime == null) {
					temp.setReason(ExceptionCode.餐类型不存在.getMsg() + ": " + productTimeName);
					tempDao.insert(temp);
					tempList.add(temp);
					continue;
				}
				
				CreateProductBo bo = new CreateProductBo();
				String foodNo = food.getFoodNo();
				Long foodId = food.getFoodId();
				bo.setType(ProductType.一周菜单.key());
				bo.setFoodNo(foodNo);
				bo.setFoodId(foodId);
				bo.setDate(date);
				bo.setFloor(floor);
				bo.setProductTime(foodTime);
				Object result = productCreate.process(bo);
				
				System.out.println(result);
				if (result instanceof ResponseSucc) {
					String reason = ((ResponseSucc) result).getMsg();
					temp.setReason(reason);
					tempDao.insert(temp);
					tempList.add(temp);
					continue;
				}
				temp.setReason(new ResponseSucc().getCode() + ":" + new ResponseSucc().getMsg());
				tempList.add(temp);
			}
			
			if (tempList.isEmpty()) {
				Temp t = new Temp();
				tempList.add(t);
			}
			
			LinkedHashMap<String,String> map = ExcelUtil.getExportWeekMenuMap();
			String fileName = "result-" + System.currentTimeMillis() + ".xls";
			response.setHeader("Content-disposition",
					"attachment; filename=" + new String(fileName.getBytes("GB2312"), "utf-8"));
			response.setContentType("application/msexcel");// 定义输出类型
			OutputStream os = response.getOutputStream();
			ExcelUtil.listToExcel(tempList, map, "导入流水表", os);
			
		} else {
			
			List<Temp> tempList = new ArrayList<Temp>();
			if (tempList.isEmpty()) {
				Temp t = new Temp();
				t.setItem0("红烧肉");
				t.setItem1("周一");
				t.setItem2("一楼");
				t.setItem3("早餐");
				tempList.add(t);
				t = new Temp();
				t.setItem0("红烧排骨");
				t.setItem1("周二");
				t.setItem2("二楼");
				t.setItem3("午餐");
				tempList.add(t);
				t = new Temp();
				t.setItem0("红烧鸡块");
				t.setItem1("周三");
				t.setItem2("四楼");
				t.setItem3("特色餐");
				tempList.add(t);
				t = new Temp();
				t.setItem0("红烧鱼块");
				t.setItem1("周四");
				t.setItem2("五楼");
				t.setItem3("晚餐");
				tempList.add(t);
			}
			
			LinkedHashMap<String,String> map = ExcelUtil.getExportWeekMenuMould();
			String fileName = "mould-" + System.currentTimeMillis() + ".xls";
			response.setHeader("Content-disposition",
					"attachment; filename=" + new String(fileName.getBytes("GB2312"), "utf-8"));
			response.setContentType("application/msexcel");// 定义输出类型
			OutputStream os = response.getOutputStream();
			ExcelUtil.listToExcel(tempList, map, "导入模板", os);
			
		}
    	
        return null;
	}

}
